/*	kd:
	
	At times, you just wanna put a long message inside of a box. With this, you
	don't really have to bother with the details.
	
	*/

struct Gc_WrapMessage {

	// kd: Deliberately not private.
	transient BrokenLines			text;
	
	void Build (String text_string, Font ffont, double wrap_width = 1) {
		text = ffont.BreakLines(text_string, wrap_width);
		// console.printf(text_string);
	}
	
	int Lines () const {
		return text ? text.count() : 0;
	}
	
	bool HasText () {
		return text != NULL && 0 < text.count();
	}
}

struct Gc_ScrollMessage {

	// kd: Deliberately not private.
	FadeTick						scroll_t;
	Array <String>					text;
	
	// kd: And some other info.
	private int						line_count;
	private int						remaining_scroll;
	private int						tics_per_line;
	private bool					is_scrolling;
	private bool					is_scrolling_down;
	private int						scroll_tics;
	private int						scroll_index;
	private int						source_line_count;
	
	void Build (int line_count_, Gc_WrapMessage message) {
		text.clear();
		
		if(0 < line_count_) {
			source_line_count = message.Lines();
			line_count = line_count_;
			text.reserve(line_count);
			LoadSection(message, start: 0);
		}
		
		scroll_index = 0;
	}
	
	void ScrollUp (int lines = 1, int tics = 5) {
		if(0 < lines && 0 < tics) {
			remaining_scroll	= lines;
			is_scrolling_down	= false;
			scroll_tics			= tics;
		}
	}
	
	void ScrollDown (int lines = 1, int tics = 5) {
		if(0 < lines && 0 < tics) {
			remaining_scroll	= lines;
			is_scrolling_down	= true;
			scroll_tics			= tics;
		}
	}
	
	void NextState (Gc_WrapMessage message) {
		scroll_t.Next();
		
		if(scroll_t.IsDone()) {
			if(0 < remaining_scroll) {
				remaining_scroll--;
				scroll_t.To(scroll_tics);
				
				if(is_scrolling_down) {
					scroll_index++;
					
					if(source_line_count <= scroll_index) {
						scroll_index = 0;
					}
				}
				
				else {
					scroll_index--;
					
					if(scroll_index < 0) {
						scroll_index = source_line_count - 1;
					}
				}
				
				LoadSection(message, start: scroll_index);
				is_scrolling = true;
			}
			
			else {
				scroll_t.Force(0);
				is_scrolling = false;
			}
		}
	}
	
	bool IsScrollingDown () const {
		return is_scrolling_down;
	}
	
	bool IsWorking () const {
		return is_scrolling;
	}
	
	int LineCount () const {
		return line_count;
	}
	
	int ScrollIndex () const {
		return scroll_index;
	}
	
	private void LoadSection (Gc_WrapMessage message, int start = 0) {
		let j = start;
		
		for(let i = 0; i < line_count; i++) {
			text [i] = message.text.StringAt(j);
			
			j++;
			
			if(source_line_count <= j) {
				j -= source_line_count;
			}
		}
	}
}
